package cate.game.role.friend;

import cate.game.GameBody;
import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.Comparator;

@NoteClass(value = "我的一个好友")
public class Friend{

    @NoteField(value = "好友的快照")
    public RoleSnapshot it;

    @NoteField(value = "拥有者唯一ID", detail = "这个好友是谁的？这个不需要持久化，也不需要发给前端，而是在消息队列的时候使用")
    public String ownerIdHex;

    public Friend() {

    }

    public Friend(Role role) {
        this.it = new RoleSnapshot(role);
        initialize();
    }

    public void initialize() {
        if (this.it != null) {
            this.it.initialize();
        }
    }

    public static Friend create(String uid, RoleSnapshot it, GameBody game){
        Friend applicant = new Friend();
        applicant.ownerIdHex = uid;
        applicant.it = it;
        applicant.it.sync(game);
        return applicant;
    }

    @Override
    public String toString() {
        return "[" + this.it.base.name + "]";
    }

    public static class Sort implements Comparator<Friend> {
        @Override
        public int compare(Friend a, Friend b) {
            final boolean asc = true;
            int onlineA = a.it.online ? 1 : 0;
            int onlineB = b.it.online ? 1 : 0;
            if (onlineA != onlineB) {
                return asc ? onlineA - onlineB : onlineB - onlineA;
            }
            if (a.it.base.power != b.it.base.power) {
                if (a.it.base.power - b.it.base.power > 0) {
                    return asc ? 1 : -1;
                } else {
                    return asc ? -1 : 1;
                }
            }
            if (a.it.base.level != b.it.base.level) {
                if (a.it.base.level - b.it.base.level > 0) {
                    return asc ? 1 : -1;
                } else {
                    return asc ? -1 : 1;
                }
            }
            return 0;
        }
    }

    private static final long NOT_ACTIVE_TIME = 24 * 3600 * 1000L;
    public boolean notActive(){
        return it == null || it.activeTime + NOT_ACTIVE_TIME < System.currentTimeMillis();
    }
}
